<template>
  <div id='app' class="flex">
    <criteria-paged :model="model" :pager='true' v-ref:paged>
      <criteria partial='criteria' @condition-changed='search' v-ref:cri>
        <div novalidate class="form-inline auto " partial >
          <p class="bg-info text-center" style="padding: 8px;">异常统计报表</p>
          <div class="flex-row auto" style="justify-content: space-between">
            <div class="form-group" v-if="!$parent.$parent.data.f_files_path">
              <label for="startDate">开始日期:</label>
              <datepicker id="startDate" placeholder="开始日期"
                          v-model="model.startDate"
                          :value.sync="model.startDate"
                          :disabled-days-of-Week="[]"
                          :format="'yyyy-MM-dd 00:00:00'"
                          :show-reset-button="reset">
              </datepicker>
              <label for="endDate">结束日期:</label>
              <datepicker id="endDate" placeholder="结束日期"
                          v-model="model.endDate"
                          :value.sync="model.endDate"
                          :disabled-days-of-Week="[]"
                          :format="'yyyy-MM-dd 23:59:59'"
                          :show-reset-button="reset">
              </datepicker>
              <label for="outlets" class="control-label">营业网点</label>
              <v-select :value.sync="model.outlets" v-model="model.outlets" @change='$parent.$parent.outletsChange'
                        :options='$parent.$parent.outlets' placeholder='营业网点'
                        close-on-select></v-select>
              <label for="operator" class="control-label">操作员</label>
              <v-select :value.sync="model.operator" v-model="model.operator"
                        :options='$parent.$parent.operator' placeholder='操作员'
                        close-on-select></v-select>


              <button class="btn btn-success" @click="search()">查询</button>
              <button class="btn btn-success" @click="$parent.$parent.confirmReport()">保存报表</button>
            </div>
            <span></span>
            <div class="form-group">
              <button class="btn btn-default" v-if="!$parent.$parent.echarts" @click="$parent.$parent.showEcharts()">显示图表</button>
              <button class="btn btn-default" v-if="$parent.$parent.echarts" @click="$parent.$parent.showEcharts()">隐藏图表</button>
              <report-print id='bussummary' top='1cm' left='2cm' width='100%' height='100%'></report-print>
              <report-excel id='bussummary' ></report-excel>
            </div>
          </div>
        </div>
      </criteria>
      <data-grid :model="model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <th>序号</th>
            <th>用户姓名</th>
            <th>表编号</th>
            <th>表品牌</th>
            <th>表型号</th>
            <th>上期底数</th>
            <th>本期底数</th>
          </tr>
        </template>
        <template partial='body'>
          <td style="text-align:center">{{$index+1}}</td>
          <td style="text-align:center">{{row.f_user_name}}</td>
          <td style="text-align:center">{{row.f_user_id}}</td>
          <td style="text-align:center">{{row.f_meter_brand}}</td>
          <td style="text-align:center">{{row.f_meter_style}}</td>
          <td style="text-align:center">{{row.f_last_tablebase}}</td>
          <td style="text-align:center">{{row.f_tablebase}}</td>
        </template>
      </data-grid>
    </criteria-paged>
    <div class="span" v-if="echarts">
      <abnormal-echarts></abnormal-echarts>
    </div>

    <modal :show.sync="show" v-ref:modal small backdrop="false">
      <header slot="modal-header" class="modal-header">
        <h4 class="modal-title">输入文件名称</h4>
      </header>
      <article slot="modal-body" class="modal-body">
        <div class="form-group">
          <input  type="text" class="form-control" v-model="filename" placeholder='保存的文件名'>
        </div>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <button v-show="show" type="button" class="btn btn-default" @click='close'>取消</button>
        <button v-show="show" type="button" class="btn btn-success" @click='confirm(filename)'>确认</button>
      </footer>
    </modal>

  </div>
</template>

<script>
  import { DataModel, HttpResetClass } from 'vue-client'
  import * as Util from '../../../Util'
  // import AppData from '../../../stores/AppData'
  import co from 'co'
  import AbnormalEcharts from './AbnormalEcharts'
  import { PagedList } from 'vue-client'

  let outletGen = function * (self) {
    let http = new HttpResetClass()
    let outlets = yield http.load('POST', '/rs/search', {source: 'tool.getChildrenOfResName($晋城市煤气开发利用中心$)', userid: ''}, {resolveMsg: null, rejectMsg: null})
    outlets.data.forEach((item, next) => {
      let temp = {}
      temp.label = item.name
      temp.value = item.name
      self.outlets.push(temp)
    })
    self.outlets = [{label: '全部', value: ''}, ...self.outlets]
  }
  let operatorGen = function * (self, val) {
    if (val[0] === '') {
      self.operator = [{label: '全部', value: ''}]
      // self.$refs.paged.$refs.criteria.model.operator[0] = ''
      console.log('11111114444444', self.$refs.paged.$refs.criteria)
    } else {
      let operators = yield self.$resetpost('/rs/search', {
        source: `tool.getChildrenOfResName($${val}$)`,
        userid: ''
      }, {resolveMsg: null, rejectMsg: null})
      operators.data.forEach((item, next) => {
        let temp = {}
        temp.label = item.name
        temp.value = item.name
        self.operator.push(temp)
      })
      self.operator = [{label: '全部', value: ''}, ...self.operator]
    }
    self.$set('$refs.paged.$refs.criteria.model.operator[0]', '')
  }


  export default {
    title: '异常统计报表',
    props: ['data'],
    components: {AbnormalEcharts},
    data () {
      return {
        model: new PagedList('rs/sql/getPartialInfo', 20),
        reportStr: null,
        show: false,
        outlets: [],
        operator: [],
        filename: '',
        echarts: true
      }
    },
    ready () {
      this.search()
    },
    methods: {
      search () {
        this.$refs.paged.$refs.cri.search()
      },
      outletsChange (val) {
        this.operator = []
        let operator = operatorGen(this, val)
        return co(operator)
      },
      getOutlets () {
        let outlet = outletGen(this)
        return co(outlet)
      },
      showEcharts () {
        this.echarts = !this.echarts
      },
      // 根据文件路径获取储存的报表内容
      getFileContent (path) {
        this.$resetpost('rs/logic/getReportFileContent', {f_files_path: path}, {resolveMsg: null, rejectMsg: null}).then((res) => {
          this.reportStr = res.data.filecontent
          this.model.state = '正确'
        })
      },
      confirm () {
        if (!this.filename || this.filename === '') {
          this.$showAlert('无法保存，文件名不能为空', 'warning', 3000)
        }
        let saveGen = saveFile(this)
        co(saveGen)
      },
      close () {
        this.show = false
      },
      // 将报表保存成文件
      confirmReport () {
        this.show = true
        this.filename = this.data.f_report_name + Util.toStandardDateString()
      }
    },
    watch: {
      'data' (val) {
        if (val.f_files_path) {
          this.getFileContent(val.f_files_path)
        } else {
          this.reportStr = null
        }
      }
    },
    computed: {
      // outlets () {
      //   return [{label: '全部', value: ''}, ...AppData.getParam('营业网点')]
      // },
      // operator () {
      //   return [{label: '全部', value: ''}, ...AppData.getParam('操作员')]
      // }
    }
  }
</script>
